<!-- 样品列表 -->
<template>
	<view class="page">
		<view class="con">
			<view class="uni-list" v-if="listData.length != 0">
				<!-- hover-class="uni-list-cell-hover" -->
				<view class="uni-list-cell" v-for="(item, index) in listData" :key="index">
					<view class="uni-media-list width-f100">
						<text class="width-a100">样品编码：{{ item && (item.is_xc == '1' ? '/' : item.code) }}</text>
						<!-- <button class="width-auto upload-btn" type="primary" @click="printer(item, index)">打印</button> -->
					</view>
					<view class="uni-media-list width-f100">
						<text class="width-a100 f-c">样品名称：{{ (item && item.sample_name) || '' }}</text>
						<text class="width-auto f-c">样品份数：{{ (item && item.number) || '' }}</text>
					</view>
					<view class="uni-media-list width-f100">
						<text class="width-a100 f-c">采样点：{{ (item && item.location_id) || '' }}</text>
						<text class="width-auto f-c">状态：{{ (item && item.state) || '' }}</text>
					</view>

					<view class="uni-media-list width-f100" v-if="item && item.sample_type != 1">
						<text class="width-a100 f-c">样品类型：{{ sampleTypes[item.sample_type] || '' }}</text>
					</view>

					<view
						class="uni-media-list width-f100"
						hover-class="uni-list-cell-hover"
						v-if="item && item.sampling_standard && item.sampling_standard[0]"
						@click="goSamplingStandard(item, index)"
					>
						<view class="uni-list-cell-navigate uni-navigate-right">
							<text class="width-auto f-c">采样标准：</text>
							<text class="width-a100 f-c">{{ (item && item.sampling_standard && item.sampling_standard[0] && item.sampling_standard[0].type_name) || '' }}</text>
						</view>
					</view>
					<!-- v-if="(item && item.state) == '采样中'" -->
					<view class="uni-media-list width-f100 btns" v-if="item && item.sample_type == 1">
						<button class="width-auto upload-btn" type="primary" @click="goDetail(item, index)">详情</button>
						<button class="width-auto upload-btn" @click="updateSample(item, index, '1')">添加质控样</button>
					</view>
					<view class="uni-media-list width-f100 btns">
						<button class="width-auto upload-btn" type="primary" @click="goDetail(item, index)" v-if="item && item.sample_type != 1">详情</button>
						<button class="width-auto upload-btn" type="warn" @click="delSample(item, index)">删除</button>
						<button class="width-auto upload-btn" @click="updateSample(item, index,null)">修改</button>
						
					
						
					</view>
				</view>
			</view>
		</view>
		<!-- <button type="primary" class="primary" @tap="addSample">添加</button> -->
	</view>
</template>
<script>
import net from '../../net.js';
import service from '../../service.js';
import utils from '../../common/utils.js';
import uploaddata from '../../uploaddata.js';
import uniIcons from '@/components/uni-icons/uni-icons.vue';

export default {
	components: {
		uniIcons
	},
	data() {
		return {
			listData: [],
			loadingType: 0,
			data: {},
			sampleTypes: {}
		}
	},
	onNavigationBarButtonTap(e) {
		this.addSample();
	},
	// 加载数据
	onLoad(res) {
		this.sampleTypes = service.SAMPLE_TYPE;
		let data = JSON.parse(res.data);
		console.log(data);
		this.codeId = data && data.code && data.code.value;
		this.listData = [];
		this.loadingType = 0;
		this.getList();
	},
	// 下拉刷新
	onPullDownRefresh() {
		this.loadingType = 0;
		this.listData = [];
		this.getList();
	},
	onShow() {
		console.log(this.codeId);
		this.getList();
	},
	methods: {
		//打印
		printer(item,index){
			console.log(item);
		},
		//数据
		getList() {
			if (this.loadingType != 0) {
				return;
			}
			this.loadingType = 1;
			net.netGet(
				'/module/api/main/statementsampleList/' + encodeURIComponent(this.codeId),
				{},
				res => {
					if (res != null && Array.isArray(res) && res.length != 0) {
						console.log(res, '______res');
						this.listData = res;
						uni.showToast({ icon: 'none', title: '正在更新数据' });
					} else {
						uni.showToast({ icon: 'none', title: '暂无数据' });
					}
				},
				() => {
					uni.showToast({ icon: 'none', title: '无法更新数据' });
				},
				(res) => {
					//console.log(JSON.stringify(res));
					this.loadingType = 0;
					uni.stopPullDownRefresh();
				}
			);
		},
		//详情
		goDetail(data, index) {
			console.log(data);
			let param = utils.toParam({ sampleId: data.id, codeId: this.codeId, listItem: data });
			uni.navigateTo({
				url: './sample_detail' + param
			});
		},
		//删除
		delSample(item, index) {
			console.log(item.id, index);
			uni.showModal({
				title: '提示',
				content: '是否删除该样品？',
				showCancel: true,
				success: res => {
					console.log(res, item.id);
					if (res.confirm) {
						//confirm为 true 时，表示用户点击了确定按钮
						net.netPost(
							'/module/api/main/deleteSample/',
							{id:item.id},
							 
							res => {
								console.log(res);
								if (res == 'success') {
									uni.showToast({ icon: 'none', title: '删除成功' });
									this.listData.splice(index, 1);
								} else {
									uni.showToast({ icon: 'none', title: '删除失败' });
								}
							},
							() => {
								uni.showToast({ icon: 'none', title: '删除失败' });
							}
						);
						
					}
				}
			});
		},
		//添加质控样和修改
		updateSample(item, index, type) {
			net.netGet(
				'/module/api/main/getEditSampleInfo/' + item.id,
				{},
				res => {
					console.log(res,"--------------res");
					let param = utils.toParam({ codeId: this.codeId, data: res, type: type });
					console.log(param);
					uni.navigateTo({ url: './sample_add' + param });
				},
				() => {
					uni.showToast({ icon: 'none', title: '无法获取样品详情' });
				},
				res => {}
			);
		},
		//采样标准
		goSamplingStandard(item, index) {
			let param = utils.toParam({ standardId: item.sampling_standard[0].id });
			uni.navigateTo({
				url: './sample_standard' + param
			});
		},
		//导航栏添加按钮事件
		addSample() {
			let param = utils.toParam({ codeId: this.codeId ,typeB:1});
			uni.navigateTo({ url: './sample_add' + param });
		}
	}
};
</script>

<style>
.page {
	padding-top: 20upx;
	height: auto;
	width: 100%;
}

page {
	background: #efeff4;
}

.uni-list-cell {
	flex-wrap: wrap;
	padding: 22upx 30upx 12upx 30upx;
}

.uni-list-cell .uni-media-list {
	padding: 0 0 5upx 0;
}

.uni-list-cell:after {
	left: 0;
}

.uni-media-list-body {
	height: auto;
	justify-content: space-around;
}

.btns {
	justify-content: space-between;
	padding: 0upx 10upx !important;
}

.btns + .btns {
	padding-top: 10upx !important;
}

.f-c {
	font-size: 32upx;
	color: #444444;
}

.uni-list-cell-navigate {
	padding: 10upx 0;
}

.uni-list-cell-navigate text {
	margin: 0 0 0 10upx;
}

.uni-list-cell-navigate .width-a100 {
	margin: 0;
}

.upload-btn {
	font-size: 32upx;
	padding: 10upx 18upx;
	line-height: 1.4;
	margin: 0;
}
</style>
